// 1.导入相关依赖包
const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')

// 2.定义数据类型
/**
 * query类型是默认客户端查询的类型
 * 该类型在服务端必须存在且唯一
 */

const typeDefs = gql`
  #学生类型
  type Student {
    id: ID
    sname: String
    gender: Boolean
  }
  #查询类型
  type Query {
    hello: String
    stu(id: Int): Student
  }
`

// 3.解析数据类型对应的具体数据
const resolvers = {
  Query: {
    hello: () => 'Hello World!',
    stu: (parent, args) => {
      console.log(args)
      if (args.id === 1) {
        return {
          id: 11,
          sname: '李素',
          gender: true
        }
      } else {
        return {
          id: 0,
          sname: '--',
          gender: false
        }
      }
    }
  }
}

// 4.整合apollo-server和express
/**
 *   typeDefs,resolvers 属性名是固定的
 */
const server = new ApolloServer({
  typeDefs,
  resolvers
})

const app = express()
server.applyMiddleware({ app })

// 5.监听启动
app.listen({ port: 4000 }, () => console.log('Now browse to http://localhost:4000' + server.graphqlPath))

/**
 * 
 query param($id:Int,$gender:Boolean!){
  hello
 stu(id:$id){
  id
  sname
  gender @skip(if: $gender)
}
}
query param($id:Int,$gender:Boolean!){
  hello
 stu(id:$id){
  id
  sname
  gender @include(if: $gender)
}
}

{
  "id": 1,
  "gender": true
}
 */
